<html>

<head>
<title>Ankiety</title>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">

</head>

<body>
<div style="text-align:center; width:1024px; height:250px; text-align:center; margin-left:auto; margin-right:auto">
<img src="Logo_admin.png"/>
<hr/>
</div>
	<?php 
	$link = mysql_connect('localhost', 'root', '', 'ankiety')
    	or die('Could not connect: ' . mysql_error());
	echo "<br>";
	mysql_select_db('ANKIETY') or die('Could not select database');
	session_start();
	
	if(isset($_POST['dodaj_odpowiedz']))
	{	
		$_SESSION['odpowiedzi']=$_SESSION['odpowiedzi']+1;
	}
	if(isset($_POST['dodaj']) || isset($_POST['dodaj_odpowiedz']))
	{
		echo "<div style='float:center; width:700; margin-left:auto; margin-right:auto'><h2>Dodajesz nową ankietę:</h2></br>";
		echo "Pytanie: ";
		echo '<form name="dodaj_ankiete" method="post">';
		if(!isset($_POST['pytanie']))
		echo "<p><input name='pytanie' type='text' value='' size='100' /> </p>";
		else
		echo "<p><input name='pytanie' type='text' value='".$_POST['pytanie']."' size='100' /> </p>";

		echo "Odpowiedzi: ";
		for ($i=1; $i <= $_SESSION['odpowiedzi']; $i++) 
				{ 
					$j='odp'.$i;
					if(!isset($_POST["$j"]))
					echo "<p> ".$i.": <input name=$j type='text' value='' size='40' /> </p>";
					else
					echo "<p> ".$i.": <input name=$j type='text' value='".$_POST["$j"]."' size='40' /> </p>";
				}
		echo "<p><input name='dodaj_odpowiedz' type='submit' value='Dodaj odpowiedź' />";
		if(isset($_POST['tryb_wyboru']))
		{
		echo "<input name='tryb_wyboru' type='checkbox' checked>Wielokrotny tryb wyboru</p>";
		}
		else
		{
		echo "<input name='tryb_wyboru' type='checkbox'>Wielokrotny tryb wyboru</p>";
		}
		echo "<p><input name='dodaj_ankiete' type='submit' value='Dodaj ankietę' style='width:280px;' />";
		echo "</form>";
		echo '<form name="powrot" method="post" action="admin.php">';
		echo "<input name='powrot' type='submit' value='Powrót do głównego panelu' style='width:280px;'/>";
		echo "</form></p></div>";
	}	
	if(isset($_POST['dodaj_ankiete']))
	{
		$query="select id_question from questions order by id_question desc";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		while ($row = mysql_fetch_array($result))
		{
				$id = $row[0];
				break;
		}
		$id++;
		if(isset($_POST['tryb_wyboru']))
		{
		$query="INSERT INTO QUESTIONS (ID_QUESTION, QUESTION, DATE, ANSWERS_COUNT, ID_USER, MULTIPLE_CHOOSE)
				VALUES ($id, '".$_POST['pytanie']."', NOW(), 0, 1, true)" ;
		}
		else
		{
		$query="INSERT INTO QUESTIONS (ID_QUESTION, QUESTION, DATE, ANSWERS_COUNT, ID_USER, MULTIPLE_CHOOSE)
				VALUES ($id, '".$_POST['pytanie']."', NOW(), 0, 1, false)" ;
		}
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		
		$query="select id_answer from answers order by id_answer desc";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		while ($row = mysql_fetch_array($result))
		{
				$id_odp = $row[0];
				break;
		}
		$id_odp++;
		for($i=1;$i<=$_SESSION['odpowiedzi'];$i++)
		{
			$j='odp'.$i;
			$query="INSERT INTO ANSWERS (ID_ANSWER, ANSWER, ID_QUESTION, ANSWERS_COUNT)
				VALUES ($id_odp, '".$_POST["$j"]."', $id, 0)" ;
			$result = mysql_query($query) or die('Query failed: ' . mysql_error());
			$id_odp++;
		}
		
		echo "<div style='float:center; width:700; margin-left:auto; margin-right:auto'>Ankieta została dodana!";
		echo '<form name="powrot" method="post" action="admin.php">';
		echo "<input name='powrot' type='submit' value='Powrót' />";
		echo "</form></div>";
	}
	?>
</body>

</html>
